package com.qs.eggs.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.user.UserInfoActivity;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class UpUserInfoActivity extends Activity implements View.OnClickListener{
    private ImageView back;
    private TextView tv,baocun;
    private EditText et;
    private String flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upd_info);
        flag=getIntent().getStringExtra("flag");
        initView();
    }
    private void initView(){
        back=(ImageView)findViewById(R.id.back);
        tv=(TextView)findViewById(R.id.tv);
        baocun=(TextView)findViewById(R.id.baocun);
        et=(EditText)findViewById(R.id.et);

        back.setOnClickListener(this);
        baocun.setOnClickListener(this);
        if (flag.equals("1")){
            tv.setText("姓名");
            SpannableString s = new SpannableString("请输入姓名");
            et.setHint(s);
        }else if(flag.equals("2")){
            tv.setText("手机");
            SpannableString s = new SpannableString("请输入手机号");
            et.setHint(s);
        }else if(flag.equals("3")){
            tv.setText("密码");
            SpannableString s = new SpannableString("请输入密码");
            et.setHint(s);
        }else if(flag.equals("4")){
            tv.setText("账号");
            SpannableString s = new SpannableString("请输入账户号");
            et.setHint(s);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                UpUserInfoActivity.this.finish();
                break;
            case R.id.baocun:
              if (flag.equals("1")){
                keepName();
              }else if (flag.equals("2")){
                keepPhone();
              }else if (flag.equals("3")){
                keepPwd();
              }else if (flag.equals("4")){
                keepZhanghu();
              }
                break;
        }
    }
    //保存姓名
    private void keepName(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("name",et.getText().toString());
        map.put("biaoshi","1");
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"personal?action=updPersonalInfo ")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("修改信息=="+s);
                        if (s.equals("\"true\"")){
                         UserInfoActivity.instance.finish();
                            UpUserInfoActivity.this.finish();
                         Intent intent=new Intent(UpUserInfoActivity.this,UserInfoActivity.class);
                         startActivity(intent);
                }
                    }
                });
    }
    //保存手机号
    private void keepPhone(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("phone",et.getText().toString());
        map.put("biaoshi","1");
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"personal?action=updPersonalInfo ")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("修改信息=="+s);
                        if (s.equals("\"true\"")){
                            UserInfoActivity.instance.finish();
                            UpUserInfoActivity.this.finish();
                            Intent intent=new Intent(UpUserInfoActivity.this,UserInfoActivity.class);
                            startActivity(intent);
                        }
                    }
                });
    }
    //保存密码
    private void keepPwd(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("password",et.getText().toString());
        map.put("biaoshi","1");
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"personal?action=updPersonalInfo ")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("修改信息=="+s);
                        if (s.equals("\"true\"")){
                            UserInfoActivity.instance.finish();
                            UpUserInfoActivity.this.finish();
                            Intent intent=new Intent(UpUserInfoActivity.this,UserInfoActivity.class);
                            startActivity(intent);
                        }
                    }
                });
    }
    //保存账户号
    private void keepZhanghu(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("accountname",et.getText().toString());
        map.put("biaoshi","1");
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"personal?action=updPersonalInfo ")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("修改信息=="+s);
                        if (s.equals("\"true\"")){
                            UserInfoActivity.instance.finish();
                            UpUserInfoActivity.this.finish();
                            Intent intent=new Intent(UpUserInfoActivity.this,UserInfoActivity.class);
                            startActivity(intent);
                        }
                    }
                });
    }
}
